#include "SpinBox.h"

RSpinBox::RSpinBox()
{
    SetNumber(0); 
    
    Mouse->Drag->bIsEnabled=true;
    Mouse->Drag->OnDragStarted.Bind([this](){
        InitailNumber=Text.Get().ToDouble();
    });

    Mouse->Drag->OnDragMoved.Bind([this](const SVec2 & TotalMoveDistance)->void{
        float NewNumber=InitailNumber+TotalMoveDistance.X*Delta;
        SetNumber(NewNumber); 
        OnNumberAccepted.Broadcast(NewNumber);
    });
}

void RSpinBox::SetNumber(double InRawNumber)
{
    double Number= CMath::Clamp(InRawNumber,MinNumber,MaxNumber);

    if(bIsInteger)
    {
       Text=CString((int64_t)Number); 
    }
    else
    {
        Text=CString(Number); 
    }
}


double RSpinBox::GetNumber()
{
    return Text.Get().ToDouble();
}

void RSpinBox::SetMinNumber(double InMinNumber)
{
    MinNumber=InMinNumber;
}

void RSpinBox::SetMaxNumber(double InMaxNumber)
{
    MaxNumber=InMaxNumber;
}
